<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryRef"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item label="优惠券id" prop="couponId">
        <el-input
          v-model="queryParams.couponId"
          placeholder="请输入优惠券id"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="会员id" prop="memberId">
        <el-input
          v-model="queryParams.memberId"
          placeholder="请输入会员id"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="会员名字" prop="memberNickName">
        <el-input
          v-model="queryParams.memberNickName"
          placeholder="请输入会员名字"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="获取方式" prop="getType">
        <el-select
          v-model="queryParams.getType"
          placeholder="请选择获取方式"
          clearable
        >
          <el-option
            v-for="dict in coupon_get_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="使用状态" prop="useType">
        <el-select
          v-model="queryParams.useType"
          placeholder="请选择使用状态"
          clearable
        >
          <el-option
            v-for="dict in coupon_use_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery"
          >搜索
        </el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Plus"
          @click="handleAdd"
          v-hasPermi="['marketing:couponHistory:add']"
          >新增</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="Edit"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['marketing:couponHistory:edit']"
          >修改</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="Delete"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['marketing:couponHistory:remove']"
          >删除</el-button
        >
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="list"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="id" align="center" prop="id" />
      <el-table-column label="优惠券id" align="center" prop="couponId" />
      <el-table-column label="会员id" align="center" prop="memberId" />
      <el-table-column label="会员名字" align="center" prop="memberNickName" />
      <el-table-column label="获取方式" align="center" prop="getType">
        <template #default="scope">
          <dict-tag :options="coupon_get_type" :value="scope.row.getType" />
        </template>
      </el-table-column>
      <el-table-column label="使用状态" align="center" prop="useType">
        <template #default="scope">
          <dict-tag :options="coupon_use_type" :value="scope.row.useType" />
        </template>
      </el-table-column>
      <el-table-column label="领取时间" align="center" prop="createTime" width="180"/>
      <el-table-column label="订单id" align="center" prop="orderId" />
      <el-table-column label="订单号" align="center" prop="orderSn" />
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template #default="scope">
          <el-button
            link
            type="primary"
            icon="Edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['marketing:couponHistory:edit']"
            >修改</el-button
          >
          <el-button
            link
            type="primary"
            icon="Delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['marketing:couponHistory:remove']"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改优惠券领取历史记录对话框 -->
    <el-dialog :title="title" v-model="open" width="500px" append-to-body>
      <el-form ref="formRef" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="优惠券id" prop="couponId">
          <el-input v-model="form.couponId" placeholder="请输入优惠券id" />
        </el-form-item>
        <el-form-item label="会员id" prop="memberId">
          <el-input v-model="form.memberId" placeholder="请输入会员id" />
        </el-form-item>
        <el-form-item label="会员名字" prop="memberNickName">
          <el-input
            v-model="form.memberNickName"
            placeholder="请输入会员名字"
          />
        </el-form-item>
        <el-form-item label="获取方式" prop="getType">
          <el-select v-model="form.getType" placeholder="请选择获取方式">
            <el-option
              v-for="dict in coupon_get_type"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="使用状态" prop="useType">
          <el-select v-model="form.useType" placeholder="请选择使用状态">
            <el-option
              v-for="dict in coupon_use_type"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="使用时间" prop="useTime">
          <el-date-picker
            clearable
            v-model="form.useTime"
            type="datetime"
            placeholder="请选择使用时间"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item label="订单id" prop="orderId">
          <el-input v-model="form.orderId" placeholder="请输入订单id" />
        </el-form-item>
        <el-form-item label="订单号" prop="orderSn">
          <el-input v-model="form.orderSn" placeholder="请输入订单号" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script setup>
import {
  listCouponHistory,
  getCouponHistory,
  delCouponHistory,
  addCouponHistory,
  updateCouponHistory,
} from "@/api/mall/server/marketing/coupon/history";
const { proxy } = getCurrentInstance();
const { coupon_use_type, coupon_get_type } = proxy.useDict(
  "coupon_use_type",
  "coupon_get_type"
);
const list = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    name: undefined,
  },
  rules: {},
});

const { queryParams, form, rules } = toRefs(data);
const getList = async () => {
  loading.value = true;
  const res = await listCouponHistory(queryParams.value);
  list.value = res.rows;
  total.value = res.total;
  loading.value = false;
};
/** 取消按钮 */
const cancel = () => {
  open.value = false;
  reset();
};
/** 表单重置 */
const reset = () => {
  form.value = {
    id: undefined,
    couponId: undefined,
    memberId: undefined,
    memberNickName: undefined,
    getType: undefined,
    useType: undefined,
    useTime: undefined,
    orderId: undefined,
    orderSn: undefined,
  };
  proxy.resetForm("formRef");
};
/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.value.pageNum = 1;
  getList();
};
/** 重置按钮操作 */
const resetQuery = () => {
  proxy.resetForm("queryRef");
  handleQuery();
};
/** 多选框选中数据 */
const handleSelectionChange = (selection) => {
  ids.value = selection.map((item) => item.couponId);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
};
/** 新增按钮操作 */
const handleAdd = () => {
  reset();
  open.value = true;
  title.value = "添加会员等级信息";
};
/**修改按钮操作 */
const handleUpdate = async (row) => {
  reset();
  const id = row.couponId || ids.value;
  const res = await getCouponHistory(id);
  form.value = res.data;
  open.value = true;
  title.value = "修改会员等级信息";
};
/** 提交按钮 */
const submitForm = () => {
  proxy.$refs["formRef"].validate((valid) => {
    if (valid) {
      if (form.value.id != undefined) {
        updateCouponHistory(form.value).then((response) => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addCouponHistory(form.value).then((response) => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
};
/** 删除按钮操作 */
const handleDelete = (row) => {
  const ids = row.id || ids.value;
  proxy.$modal
    .confirm('是否确认删除编号为"' + ids + '"的数据项？')
    .then(function () {
      return delCouponHistory(ids);
    })
    .then(() => {
      getList();
      proxy.$modal.msgSuccess("删除成功");
    })
    .catch(() => {});
};

getList();
// export default {
//   name: "CouponHistory",
//   dicts: ["coupon_get_type", "coupon_use_type"],
//   data() {
//     return {
//       // 遮罩层
//       loading: true,
//       // 选中数组
//       ids: [],
//       // 非单个禁用
//       single: true,
//       // 非多个禁用
//       multiple: true,
//       // 显示搜索条件
//       showSearch: true,
//       // 总条数
//       total: 0,
//       // 优惠券领取历史记录表格数据
//       couponHistoryList: [],
//       // 弹出层标题
//       title: "",
//       // 是否显示弹出层
//       open: false,
//       // 查询参数
//       queryParams: {
//         pageNum: 1,
//         pageSize: 10,
//         couponId: undefined,
//         memberId: undefined,
//         memberNickName: undefined,
//         getType: undefined,
//         useType: undefined,
//         useTime: undefined,
//         orderId: undefined,
//         orderSn: undefined,
//       },
//       // 表单参数
//       form: {},
//       // 表单校验
//       rules: {},
//     };
//   },
//   created() {
//     this.getList();
//   },
//   methods: {
//     /** 查询优惠券领取历史记录列表 */
//     getList() {
//       this.loading = true;
//       listCouponHistory(this.queryParams).then((response) => {
//         this.couponHistoryList = response.rows;
//         this.total = response.total;
//         this.loading = false;
//       });
//     },
//     // 取消按钮
//     cancel() {
//       this.open = false;
//       this.reset();
//     },
//     // 表单重置
//     reset() {
//       this.form = {
//         id: undefined,
//         couponId: undefined,
//         memberId: undefined,
//         memberNickName: undefined,
//         getType: undefined,
//         createTime: undefined,
//         useType: undefined,
//         useTime: undefined,
//         orderId: undefined,
//         orderSn: undefined,
//       };
//       this.resetForm("form");
//     },
//     /** 搜索按钮操作 */
//     handleQuery() {
//       this.queryParams.pageNum = 1;
//       this.getList();
//     },
//     /** 重置按钮操作 */
//     resetQuery() {
//       this.resetForm("queryForm");
//       this.handleQuery();
//     },
//     // 多选框选中数据
//     handleSelectionChange(selection) {
//       this.ids = selection.map((item) => item.id);
//       this.single = selection.length !== 1;
//       this.multiple = !selection.length;
//     },
//     /** 新增按钮操作 */
//     handleAdd() {
//       this.reset();
//       this.open = true;
//       this.title = "添加优惠券领取历史记录";
//     },
//     /** 修改按钮操作 */
//     handleUpdate(row) {
//       this.reset();
//       const id = row.id || this.ids;
//       getCouponHistory(id).then((response) => {
//         this.form = response.data;
//         this.open = true;
//         this.title = "修改优惠券领取历史记录";
//       });
//     },
//     /** 提交按钮 */
//     submitForm() {
//       this.$refs["form"].validate((valid) => {
//         if (valid) {
//           if (this.form.id != undefined) {
//             updateCouponHistory(this.form).then((response) => {
//               this.$modal.msgSuccess("修改成功");
//               this.open = false;
//               this.getList();
//             });
//           } else {
//             addCouponHistory(this.form).then((response) => {
//               this.$modal.msgSuccess("新增成功");
//               this.open = false;
//               this.getList();
//             });
//           }
//         }
//       });
//     },
//     /** 删除按钮操作 */
//     handleDelete(row) {
//       const ids = row.id || this.ids;
//       this.$modal
//         .confirm('是否确认删除优惠券领取历史记录编号为"' + ids + '"的数据项？')
//         .then(function () {
//           return delCouponHistory(ids);
//         })
//         .then(() => {
//           this.getList();
//           this.$modal.msgSuccess("删除成功");
//         })
//         .catch(() => {});
//     },
//     /** 导出按钮操作 */
//     handleExport() {
//       this.download(
//         "marketing/couponHistory/export",
//         {
//           ...this.queryParams,
//         },
//         `couponHistory_${new Date().getTime()}.xlsx`
//       );
//     },
//   },
// };
</script>
